package com.tongzhan.service.impl;

import com.tongzhan.data.vo.ReligionVO;
import com.tongzhan.data.vo.SiteVO;
import com.tongzhan.data.vo.StreetVO;
import com.tongzhan.data.vo.VillageVO;
import com.tongzhan.entity.religion.*;
import com.tongzhan.repository.religion.*;
import com.tongzhan.service.StreetService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class StreetServiceImpl implements StreetService {

    @Autowired
    private StreetRepository streetRepository;

    @Autowired
    private SiteInfoRepository siteInfoRepository;

    @Autowired
    private ReligionRepository religionRepository;

    @Autowired
    private VillageRepository villageRepository;

    @Override
    public List<StreetVO> getStreetList() {
        //查询条件
        Query query = new Query();
        query.with(Sort.by(Sort.Direction.ASC, "Sort"));
        //街道列表
        List<Street> streetList = streetRepository.findAll(query);
        //封装返回数据
        List<StreetVO> streetVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(streetList)) {
            for (Street street : streetList) {
                StreetVO streetVO = new StreetVO();
                streetVO.setStreetCode(street.getStreetCode());
                streetVO.setStreetName(street.getStreetName());
                streetVOS.add(streetVO);
            }
        }
        return streetVOS;
    }

    @Override
    public List<SiteVO> getSiteList(String streetCode) {
        //查询条件
        Query query = new Query();
        query.addCriteria(Criteria.where("Jurisdiction").is("是"));//受管辖的场所
        query.addCriteria(Criteria.where("StreetCode").is(streetCode));
        query.with(Sort.by(Sort.Direction.ASC, "SiteCode"));
        //场所列表
        List<SiteInfo> siteInfos = siteInfoRepository.findAll(query);
        //封装返回数据
        List<SiteVO> siteVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(siteInfos)) {
            for (SiteInfo info : siteInfos) {
                SiteVO siteVO = new SiteVO();
                siteVO.setSiteCode(info.getSiteCode());
                siteVO.setSiteName(info.getSiteName());
                siteVOS.add(siteVO);
            }
        }
        return siteVOS;
    }

    @Override
    public Street selectByCode(String streetCode) {
        return streetRepository.findByPrimaryKey(streetCode);
    }

    @Override
    public List<ReligionVO> getReligionList() {
        Query query = new Query();
        List<String> codes = Arrays.asList("00", "05");
        query.addCriteria(Criteria.where("ReligionCode").nin(codes));
        query.with(Sort.by(Sort.Direction.ASC, "ReligionCode"));
        List<Religion> religionList = religionRepository.findAll(query);
        //VO
        List<ReligionVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(religionList)) {
            for (Religion religion : religionList) {
                ReligionVO vo = new ReligionVO();
                vo.setReligionCode(religion.getReligionCode());
                vo.setReligionName(religion.getReligionName());
                vos.add(vo);
            }
        }
        return vos;
    }

    @Override
    public List<VillageVO> getVillageList(String streetCode) {
        Query query = new Query();
        query.addCriteria(Criteria.where("StreetCode").is(streetCode));
        query.with(Sort.by(Sort.Direction.ASC, "VillageCode"));
        List<Village> villageList = villageRepository.findAll(query);
        //VO
        List<VillageVO> villageVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(villageList)) {
            for (Village village : villageList) {
                VillageVO vo = new VillageVO();
                vo.setVillageCode(village.getVillageCode());
                vo.setVillageName(village.getVillageName());
                villageVOS.add(vo);
            }
        }
        return villageVOS;
    }

    @Override
    public List<SiteVO> siteList(String villageCode, String dutiesCode) {
        //查询条件
        Query query = new Query();
        query.addCriteria(Criteria.where("Jurisdiction").is("是"));//受管辖的场所
        query.addCriteria(Criteria.where("VillageCode").is(villageCode));
        if (StringUtils.isNotEmpty(dutiesCode)) {
            if ("04".equals(dutiesCode)) {
                //宗教场所
                query.addCriteria(Criteria.where("Type").is(0));
            } else {
                //民间信仰点
                query.addCriteria(Criteria.where("Type").is(1));
            }
        }
        //场所列表
        List<SiteInfo> siteInfos = siteInfoRepository.findAll(query);
        //封装返回数据
        List<SiteVO> siteVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(siteInfos)) {
            for (SiteInfo info : siteInfos) {
                SiteVO siteVO = new SiteVO();
                siteVO.setSiteCode(info.getSiteCode());
                siteVO.setSiteName(info.getSiteName());
                siteVOS.add(siteVO);
            }
        }
        return siteVOS;
    }

    @Override
    public List<SiteVO> getSiteList() {
        //查询条件
        Query query = new Query();
        query.addCriteria(Criteria.where("Jurisdiction").is("是"));//受管辖的场所
        query.with(Sort.by(Sort.Direction.ASC, "ReligionCode", "Sort"));
        //场所列表
        List<SiteInfo> siteInfos = siteInfoRepository.findAll(query);
        //封装返回数据
        List<SiteVO> siteVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(siteInfos)) {
            for (SiteInfo info : siteInfos) {
                SiteVO siteVO = new SiteVO();
                siteVO.setSiteCode(info.getSiteCode());
                siteVO.setSiteName(info.getSiteName());
                siteVOS.add(siteVO);
            }
        }
        return siteVOS;
    }

    @Override
    public List<SiteVO> getSiteListByReligion(String religionCode) {
        //查询条件
        Query query = new Query();
        query.addCriteria(Criteria.where("Jurisdiction").is("是"));//受管辖的场所
        query.with(Sort.by(Sort.Direction.ASC, "SiteCode"));
        if (StringUtils.isNotEmpty(religionCode)) {
            query.addCriteria(Criteria.where("ReligionCode").is(religionCode));
        }
        //场所列表
        List<SiteInfo> siteInfos = siteInfoRepository.findAll(query);
        //封装返回数据
        List<SiteVO> siteVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(siteInfos)) {
            for (SiteInfo info : siteInfos) {
                SiteVO siteVO = new SiteVO();
                siteVO.setSiteCode(info.getSiteCode());
                siteVO.setSiteName(info.getSiteName());
                siteVOS.add(siteVO);
            }
        }
        return siteVOS;
    }
}
